/*
	联盟的操作处理程序
*/

package net.sf.odinms.net.channel.handler;

import java.rmi.RemoteException;
import net.sf.odinms.client.MapleCharacter;
import net.sf.odinms.client.MapleClient;
import net.sf.odinms.client.MaplePet;
import net.sf.odinms.net.AbstractMaplePacketHandler;
import net.sf.odinms.net.channel.ChannelServer;
import net.sf.odinms.net.world.guild.MapleAlliance;
import net.sf.odinms.tools.Guild.ALLIANCE_msg;
import net.sf.odinms.tools.MaplePacketCreator;
import net.sf.odinms.tools.data.input.SeekableLittleEndianAccessor;

/**
 *
 * @author XoticStory
 */
public class AllianceOperationHandler extends AbstractMaplePacketHandler {

    @Override
    public void handlePacket(SeekableLittleEndianAccessor slea, MapleClient c) {
        System.out.println(slea.toString());

        MapleAlliance alliance = null;
        if (c.getPlayer().getGuild() != null && c.getPlayer().getGuild().getAllianceId() > 0) {
            try {
                alliance = c.getChannelServer().getWorldInterface().getAlliance(c.getPlayer().getGuild().getAllianceId());
            } catch (RemoteException rawr) {
                c.getChannelServer().reconnectWorld();
            }
        }
        if (alliance == null) {
            c.getPlayer().dropMessage("系统错误 !");
            c.getSession().write(MaplePacketCreator.enableActions());
            return;
        } else if (c.getPlayer().getMGC().getAllianceRank() > 2 || !alliance.getGuilds().contains(c.getPlayer().getGuildId())) {
            c.getSession().write(MaplePacketCreator.enableActions());
            return;
        }

        try {

            switch (slea.readByte()) {
                case 0x0A:
                    String notice = slea.readMapleAsciiString();
                    c.getChannelServer().getWorldInterface().setAllianceNotice(alliance.getId(), notice);
                    c.getChannelServer().getWorldInterface().allianceMessage(alliance.getId(), ALLIANCE_msg.联盟公告(alliance.getId(), notice), -1, -1);
                    break;
                case 0x07://联盟主改变
                    int cid = slea.readInt();
                    
                    break;
                case 0x08:
                    String ranks[] = new String[5];
                    for (int i = 0; i < 5; i++) {
                        ranks[i] = slea.readMapleAsciiString();
                    }
                    c.getChannelServer().getWorldInterface().setAllianceRanks(alliance.getId(), ranks);
                    c.getChannelServer().getWorldInterface().allianceMessage(alliance.getId(), ALLIANCE_msg.联盟地位改变(alliance.getId(), ranks), -1, -1);
                    break;
                case 0x06://被退出联盟
                    int 家族id = slea.readInt();
                    int 联盟id = slea.readInt();
                    break;
                case 0x03:
                    c.getPlayer().dropMessage("Feature not available");
                    // fall through to default.
                    String charName = slea.readMapleAsciiString();
                    int channel = c.getChannelServer().getWorldInterface().find(charName);
                if (channel == -1) {
                    c.getPlayer().dropMessage("找不到家族");
                } else {
                    MapleCharacter victim = ChannelServer.getInstance(channel).getPlayerStorage().getCharacterByName(charName);
                if (victim.getGuildId() == 0)
                    c.getPlayer().dropMessage("没有家族");
                else if(victim.getGuildRank() != 1)
                    c.getPlayer().dropMessage("他不是家族的族长.");// 还没有开始写邀请联盟.
                }
                break;
                case 0x02:
                    c.getPlayer().getGuild().setAllianceId(0);
                    c.getChannelServer().getWorldInterface().clearGuilds();//重载家族
                    c.getChannelServer().getWorldInterface().allianceMessage(alliance.getId(), ALLIANCE_msg.removeGuildFromAlliance(alliance,c.getPlayer().getGuild().getId(), c),-1,-1);
                    c.getChannelServer().getWorldInterface().clearGuilds();//重载家族
                    respawnPlayer(c.getPlayer());
                    break;
                default:
                    c.getPlayer().dropMessage("Feature not available");
            }
            alliance.saveToDB();
        } catch (RemoteException rawr) {
            c.getChannelServer().reconnectWorld();
        }
    }
    
    private void respawnPlayer(MapleCharacter mc) {
        mc.getMap().broadcastMessage(mc, MaplePacketCreator.removePlayerFromMap(mc.getId()), false);
        mc.getMap().broadcastMessage(mc, MaplePacketCreator.spawnPlayerMapobject(mc), false);
        if (mc.getNoPets() > 0) {
            for (MaplePet pet : mc.getPets()) {
                if (pet != null) {
                    mc.getMap().broadcastMessage(mc, MaplePacketCreator.showPet(mc, pet, false, false), false);
                }
            }
        }
    }
}